package com.example.pxxy_news.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pxxy_news.R;
import com.example.pxxy_news.fragment.FragmentArticleData;
import com.example.pxxy_news.fragment.FragmentArticleManager;
import com.example.pxxy_news.fragment.FragmentArticleNotice;

public class ArticleManagerActivity extends BaseActivity implements
		OnClickListener {
	FragmentManager fm;
	FragmentTransaction ft;
	TextView tv_am_article, tv_am_notice, tv_am_data;
	ImageView image_article_edit;
	Fragment fragment_article, fragment_notice, fragment_data;
	ImageView image_back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_article_manager);
		initView();
		fragment_article = new FragmentArticleManager();
		ft.replace(R.id.am_content, fragment_article);
		tv_am_article.setTextColor(Color.RED);
		ft.commit();
	}

	private void initView() {
		tv_am_article = (TextView) findViewById(R.id.tv_am_article);
		tv_am_notice = (TextView) findViewById(R.id.tv_am_notice);
		tv_am_data = (TextView) findViewById(R.id.tv_am_data);
		image_back = (ImageView) findViewById(R.id.image_am_back);
		image_article_edit = (ImageView) findViewById(R.id.image_am_edit);
		tv_am_article.setOnClickListener(this);
		tv_am_notice.setOnClickListener(this);
		tv_am_data.setOnClickListener(this);
		image_back.setOnClickListener(this);
		image_article_edit.setOnClickListener(this);

		fm = getFragmentManager();
		ft = fm.beginTransaction();
	}

	@Override
	public void onClick(View v) {
		fm = getFragmentManager();
		ft = fm.beginTransaction();
		switch (v.getId()) {
		case R.id.tv_am_article:
			hideFragment(ft);
			fragment_article = new FragmentArticleManager();
			ft.replace(R.id.am_content, fragment_article);
			tv_am_article.setTextColor(Color.RED);
			tv_am_notice.setTextColor(Color.BLACK);
			tv_am_data.setTextColor(Color.BLACK);
			ft.commit();
			break;
		case R.id.tv_am_notice:
			hideFragment(ft);
			fragment_notice = new FragmentArticleNotice();
			ft.replace(R.id.am_content, fragment_notice);
			tv_am_article.setTextColor(Color.BLACK);
			tv_am_notice.setTextColor(Color.RED);
			tv_am_data.setTextColor(Color.BLACK);
			ft.commit();
			break;
		case R.id.tv_am_data:
			hideFragment(ft);
			fragment_data = new FragmentArticleData();
			ft.replace(R.id.am_content, fragment_data);
			tv_am_article.setTextColor(Color.BLACK);
			tv_am_notice.setTextColor(Color.BLACK);
			tv_am_data.setTextColor(Color.RED);
			ft.commit();
			break;
		case R.id.image_am_edit:
			Intent in = new Intent(ArticleManagerActivity.this,EditArticleActivity.class);
			startActivity(in);
			break;
		case R.id.image_am_back:
			this.finish();
		default:
			break;
		}

	}

	private void hideFragment(FragmentTransaction ft) {
		if (fragment_article != null) {
			ft.remove(fragment_article);
		}
		if (fragment_notice != null) {
			ft.remove(fragment_notice);
		}
		if (fragment_data != null) {
			ft.remove(fragment_data);
		}
	}
}
